<?php

namespace app\models;

class User extends \yii\base\Object implements \yii\web\IdentityInterface
{
    public $id;
    public $username;
    public $password;
    public $role_id;
    public $authKey;

   /*  private static $users = [
        '100' => [
            'id' => '100',
            'username' => 'admin',
            'password' => 'admin',
            'authKey' => 'test100key',
            'accessToken' => '100-token',
        ],
        '101' => [
            'id' => '101',
            'username' => 'demo',
            'password' => 'demo',
            'authKey' => 'test101key',
            'accessToken' => '101-token',
        ],
    ]; */    

    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
    	$user = new User();
    	 
    	$result = Admin::findOne($id);
    	 
    	if(!empty($result))
    	{
    		$user->id = $result->id;
    	
    		$user->username = $result->adm_username;
    	
    		$user->password = $result->adm_password;
    		
    		$user->role_id = $result->role_id;
     	}

    	return $user;   
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        /* foreach (self::$users as $user) {
            if ($user['accessToken'] === $token) {
                return new static($user);
            }
        }
 */
        return null;
    }

    /**
     * Finds user by username
     *
     * @param  string      $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
    	//查询用户信息
    	$user = new User();
    	
    	$result = Admin::find()->where(['adm_username' => $username])->one();
    	
    	if(!empty($result))
    	{
    		$user->id = $result->id;
    		
    		$user->username = $result->adm_username;
    		
    		$user->password = $result->adm_password; 
    		
    		$user->role_id = $result->role_id;
    	}
    	
    	return $user;
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->authKey;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return $this->authKey === $authKey;
    }

    /**
     * Validates password
     *
     * @param  string  $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password)
    {
        return $this->password === $password;
    }
}
